/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import MaterialPack.*;
import Supporter.*;
import javax.obex.HeaderSet;
/**
 *
 * @author Van persie
 */
public class MenuCanvas extends Canvas implements Runnable,CommandListener
{
    private MainMIDlet main = null;
    
    private Image background = null;
    private Vector buttons = null;
    
    private int height,width;

    public MenuCanvas(MainMIDlet midlet) {
        main = midlet;
        height = this.getHeight();
        width = this.getWidth();
        
        try{
            LoadResource( );            
        }catch(Exception e){}
        
      //this.sizeChanged(UP, UP);  
    }

    public void LoadResource() throws IOException {
        if(buttons != null)
            buttons.removeAllElements();
        buttons = new Vector();
        ImageSupporter imgSupporter = new ImageSupporter();
        //background = ImageSupporter.scaleImage(  Image.createImage("/Background.png"),width,height);
        Image temp = imgSupporter.getImage("/Background.png");
        background = ImageSupporter.scaleImage(temp, width, height);
        
        buttons.addElement(new Button("/MenuIcon01.png", "/MenuIcon02.png", "/MenuIcon02.png", null, 10,4* height/8 , width/4,height/8));
        buttons.addElement( new Button("/MenuIcon03.png", "/MenuIcon04.png", "/MenuIcon04.png", null, 10, 5*height/8 , width/4,height/8));
        buttons.addElement( new Button("/MenuIcon07.png", "/MenuIcon08.png", "/MenuIcon08.png", null,  10, 6*height/8 , width/4,2*height/8));
        
        buttons.addElement( new CheckedButton("/NormalSound.png", "/PressedSound.png", "/PressedSound.png", "/Uncheck.png", width - 10 - 50, height - 10 - 50, 50, 50));
    }
    protected void sizeChanged(int w, int h)
    {
        width = w;
        height = h;
        try{
            LoadResource( );            
        }catch(Exception e){}
    }
    protected void pointerPressed(int x, int y)
    {
         for(int i=0; i <buttons.size(); i++)
             ((Button)buttons.elementAt(i)).Press(x,y);
         repaint();
    }
    protected void pointerReleased (int x, int y)
    {
        for(int i=0; i <buttons.size(); i++)
             if(((Button)buttons.elementAt(i)).Release(x,y))
                 if(((Button)buttons.elementAt(i)).isContained(x, y) == true)
                 {
                     switch(i){
                         case 0:
                             main.SwitchTo(MainMIDlet.SCENE_MANAGEMENT);
                             break;
                         case 2:
                             main.exitMIDlet();
                             break;
                             
                     }
                 }
        repaint();
    }
    protected void keyPressed(int keyCode)
    {
        
        switch(this.getKeyCode(keyCode))
        {
            case UP:
                main.destroyApp(true);
                break;
        }
    }

    public void commandAction(Command command, Displayable displayable)
    {
    }
    
    
    public void paint(Graphics g )
    {
        g.drawImage(background, 0, 0,Graphics.TOP | Graphics.LEFT);
        for(int i=0; i <buttons.size(); i++)
            ((Button)buttons.elementAt(i)).paint(g);
        
    }
    public void run()
    {
    }
}
